package com.flycent.hqhs.ui;

import java.lang.ref.WeakReference;
import java.util.List;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.flycent.hqhs.R;
import com.flycent.hqhs.bean.Logistics;
import com.flycent.hqhs.http.WebClient;
import com.flycent.hqhs.http.WebException;
import com.flycent.hqhs.ui.view.AbsViewHolderAdapter;

/**
 * @author fbl
 *  选择配送方式
 */
public class ChoiceDistributionMethodActivity extends BackableTitleBarActivity{
	@ViewInject(id = R.id.distribution_method_list)ListView mlist; 

	private DistributionAdapter mAdapter;
	private List<Logistics> aa;
	private int itemCheckId;
	private int weight;
	private long areaId;
	private String payPrice;
	private Handler mHandler = new MyHandler(this);
	private View convertView;

	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_choice_distribution_method);
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle(R.string.title_choise_distribution_method);
		itemCheckId = getIntent().getIntExtra("checkId", 0);
		weight = getIntent().getIntExtra("weight", 0);
		areaId = getIntent().getLongExtra("areaId", 0);
		payPrice = getIntent().getStringExtra("payPrice");
		
		convertView =  (View) LayoutInflater.from(ChoiceDistributionMethodActivity.this).inflate(R.layout.item_choice_distribution_buttom_lin, null);
		convertView.findViewById(R.id.choice_distribution_sure_but).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				Intent intent = new Intent();
				intent.putExtra("checkId", itemCheckId);
				intent.putExtra("mDistributionName", mAdapter.getData().get(itemCheckId).getShippingMethod().getName());
				intent.putExtra("mDistributionID", mAdapter.getData().get(itemCheckId).getShippingMethod().getId()+"");
				intent.putExtra("mDistributionPrice", mAdapter.getData().get(itemCheckId).getPrice());
				setResult(300,intent);
				finish();
			
			}
		});
		
		mlist.addFooterView(convertView);
		mAdapter = new DistributionAdapter(this, R.layout.item_choice_distribution);
		mlist.setAdapter(mAdapter);
		
		setData();
		mlist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent,
					View view, int position, long id) {
				if(position != itemCheckId ){
					itemCheckId = position;
				}
				mlist.setItemChecked(position, true);	
			}

		});

	}

	private void setData() {
		
		showLoadingDialog("");

		new Thread(){
			public void run() {
				try {
					aa = WebClient.getInstance(ChoiceDistributionMethodActivity.this).deliveryPrice(areaId,weight,payPrice);
					mHandler.sendEmptyMessage(0);
				} catch (WebException e) {
					e.printStackTrace();
				}
			};

		}.start();

	}
	@SuppressLint("HandlerLeak") 
	private class MyHandler extends Handler{
		private WeakReference<Activity> mActivity;

		private MyHandler(Activity activity){
			mActivity = new WeakReference<Activity>(activity);
		}

		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			if( mActivity.get() != null ){
				switch (msg.what) {
				case 0:
					mAdapter.update(aa);
					mlist.setItemChecked(itemCheckId, true);	
					dismissLoadingDialog();
					break;
				default:
					break;
				}
			}
		}
	}


	public class DistributionAdapter extends AbsViewHolderAdapter<Logistics>{

		public DistributionAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(int pos, Logistics itemData) {
			TextView mText = getViewFromHolder(R.id.item_choice_distribution_text);
			TextView mText1 = getViewFromHolder(R.id.item_choice_distribution_text1);
			TextView mText2 = getViewFromHolder(R.id.item_choice_distribution_text2);
			ImageView mImage = getViewFromHolder(R.id.item_choice_distribution_image);
			mText.setText(itemData.getShippingMethod().getName());
			mText1.setText("￥"+itemData.getPrice());
			mText2.setText("预计"+itemData.getDays()+"天后到达");
			if(pos  == itemCheckId){
				mImage.setImageResource(R.drawable.shopcar_check_in_one);
			}else {
				mImage.setImageResource(R.drawable.shopcar_check_one);
			}
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mHandler.removeCallbacksAndMessages(null);
	}

}
